#!/system/bin/sh
PROGRAM=${0##*/}

#reset npu
/vendor/bin/npu_powerctrl -i
/vendor/bin/npu_powerctrl -o
sleep 1

if [ $# -ne 4 ]; then 
	echo 'Usage: '$PROGRAM' loader uboot trust boot'
	exit
fi
DIR="/vendor/etc/npu_fw"
UPGRADE_TOOL=/vendor/bin/upgrade_tool

LOADER=$DIR/$1
UBOOT=$DIR/$2
TRUST=$DIR/$3
BOOT=$DIR/$4
UBOOT_ADDR=0x40000
TRUST_ADDR=0x40800
BOOT_ADDR=0x20000

function download_func()
{
	local RET1=1
	echo 'start to download loader...' >>  /data/npu.log
	$UPGRADE_TOOL db $LOADER > /dev/null
	if [ $? -ne 0 ]; then
		echo 'failed to download loader!' >>  /data/npu.log
		return $RET1;
	fi
	echo 'download loader ok' >>  /data/npu.log

	sleep 1
	echo 'start to wait loader...' >>  /data/npu.log
	$UPGRADE_TOOL td > /dev/null
	if [ $? -ne 0 ]; then
		echo 'failed to wait loader!' >>  /data/npu.log
		return $RET1
	fi
	echo 'loader is ready'  >>  /data/npu.log

	echo 'start to write uboot...' >>  /data/npu.log
	$UPGRADE_TOOL wl $UBOOT_ADDR $UBOOT > /dev/null
	if [ $? -ne 0 ]; then
		echo 'failed to write uboot!' >>  /data/npu.log
		return $RET1
	fi
	echo 'write uboot ok' >>  /data/npu.log

	echo 'start to write trust...'
	$UPGRADE_TOOL wl $TRUST_ADDR $TRUST > /dev/null
	if [ $? -ne 0 ]; then
		echo 'failed to write trust!' >>  /data/npu.log
		return $RET1
	fi
	echo 'write trust ok' >>  /data/npu.log

	echo 'start to write boot...' >>  /data/npu.log
	$UPGRADE_TOOL wl $BOOT_ADDR $BOOT > /dev/null
	if [ $? -ne 0 ]; then
		echo 'failed to write boot!' >>  /data/npu.log
		return $RET1
	fi
	echo 'write boot ok' >>  /data/npu.log
	RET1=0
	return $RET1
}

function check_device_ready_func()
{
	echo 'start to wait device...' >  /data/npu.log
	local i=0
	local RET=1
	while [ $i -lt 10 ]; do
		$UPGRADE_TOOL ld > /dev/null
		if [ $? -ne 0 ]; then
			i=$((i+1))
			echo $i
			sleep 0.1
		else
			sleep 0.1
			break
		fi
		if [ $i -eq 5 ]; then
			/vendor/bin/npu_powerctrl -o
			sleep 3
			echo 'reset npu to retry!!!' >> /data/npu.log
		fi
	done
	if [ $i -ge 10 ]; then
		echo 'failed to wait device!'  >>  /data/npu.log
		return $RET
	fi
	echo 'device is ready' >>  /data/npu.log
	RET=0
	return $RET
}

function poweron_Npu_func()
{
	/vendor/bin/npu_powerctrl -i
	sleep 0.1
	/vendor/bin/npu_powerctrl -o
	sleep 1
}

if [ ! -f $UPGRADE_TOOL ]; then
	echo $UPGRADE_TOOL 'is not existed!'
	exit
fi

if [ ! -f $LOADER ]; then
	echo $LOADER 'is not existed!'
	exit
fi

if [ ! -f $UBOOT ]; then
	echo $UBOOT 'is not existed!'
	exit
fi

if [ ! -f $TRUST ]; then
	echo $TRUST 'is not existed!'
	exit
fi

if [ ! -f $BOOT ]; then
	echo $BOOT 'is not existed!'
	exit
fi

check_device_ready_func
if [ $? = 1 ];then
	echo "check_device_ready error!!!" >> /data/npu.log
	poweron_Npu_func
	check_device_ready_func
fi

download_func
if [ $? = 1 ];then
	echo "reset download_func"
	echo 'down load error!!! reset usb hub' >> /data/npu.log
	echo 0 > /sys/class/gpio/gpio6/value
	sleep 3
	echo 1 > /sys/class/gpio/gpio6/value
	
	poweron_Npu_func
	check_device_ready_func
	download_func
fi

echo 'start to run system...' >>  /data/npu.log
$UPGRADE_TOOL rs $UBOOT_ADDR $TRUST_ADDR $BOOT_ADDR $UBOOT $TRUST $BOOT > /dev/null
if [ $? -ne 0 ]; then
	echo 'failed to run system!' >>  /data/npu.log
	exit
fi
echo 'run system ok' >>  /data/npu.log
